package com.wangcw.minicat.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.wangcw.minicat.bean.ServerXmlConfigBean;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * 读取xml信息
 */
public class XMLReadUtil {

    //配置文件名
    private static String CONFIG_NAME = "/Users/changweiwang/Documents/code/码云/lagou_21/Minicat/webapp/server.xml";

    //获取配置文件实体
    public ServerXmlConfigBean getConfigServerBean(){
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        InputStream resourceAsStream = FileUtil.getInputStream(CONFIG_NAME);
        SAXReader saxReader = new SAXReader();

        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//server");
            for (Element element : selectNodes) {
                Element selectSingleNode = (Element) element.selectSingleNode("port");
                String port = selectSingleNode.getStringValue();
                return new ServerXmlConfigBean(Integer.parseInt(port));
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }



}
